<template>
  <div class="login" style="height:200px">
    <header class="login-header">
      <div class="container m-top-20">
        <RouterLink
            class="entry"
            to="/"
            style="font-size:20px;float:right;color:aliceblue"
        >
          进入网站首页
        </RouterLink>
      </div>
    </header>
    <nav class="login">
      <a href="javascript:;">账户登录</a>
    </nav>
    <div
        class="username-box"
        style="
        border: 1px solid #99a9bf;
        margin: 100px auto;
        width: 600px;
        height: 300px;
        border-radius: 20px;
      "
    >
      <div class="form" style="margin: 40px auto; width: 500px">
        <el-form
            :model="form"
            :rules="rules"
            label-position="right"
            label-width="60px"
            status-icon
            ref="formRef"
        >
          <el-form-item label="账户" prop="username">
            <el-input v-model="form.username"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password"/>
          </el-form-item>
          <el-form-item label-width="22px" prop="agree">
            <el-checkbox size="large" v-model="form.agree">
              我已同意隐私条款和服务条款
            </el-checkbox>
          </el-form-item>
          <el-button type="primary" round size="large" class="subBtn" @click="dologin()">点击登录</el-button>
          <el-link type="primary" @click="$router.push('/register')">没有账户？去注册</el-link>

        </el-form>
      </div>
    </div>
    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
      </div>
    </footer>
  </div>
</template>
<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";
import {useRouter} from "vue-router";
import {useUserStore} from '@/store/user.js'

const userStore = useUserStore()
const form = ref({
  username: "",
  password: "",
  agree: true,
})

const rules = {
  username: [{required: true, message: "用户名不能为空", trigger: "blur"}],
  password: [
    {required: true, message: "密码不能为空", trigger: "blur"},
    {min: 4, max: 14, message: "密码长度为4~14个字符", trigger: "blur"},
  ],
  agree: [
    {
      validator: (rule, value, callback) => {
        if (value) {
          callback()
        } else {
          callback(new Error('请同意该协议'))
        }
      },
    },
  ],

};
const formRef = ref(null)
const router = useRouter()

const dologin = () => {
  const username = form.value.username
  const password = form.value.password
  formRef.value.validate(async (valid) => {
    if (valid) {
      const res = await userStore.getUserInfo({username, password})
      console.log(res)
      if(res.code ==200){
        ElMessage({type:'success',message:res.message})
        router.push({path:'/'})
      }else {
        ElMessage({type:'error',message:res.message})
        userStore.loginInfo={}
      }
    }
  })
}


</script>
<style scoped lang='scss'>


.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;
  background: url('@/assets/images/login-bg.jpg') no-repeat center / cover;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;

    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    float: right;
    width: 180px;
    margin-bottom: 30px;
    font-size: 16px;

    i {
      font-size: 14px;

      letter-spacing: -5px;
    }
  }
}

.login-section {

  height: 488px;
  position: relative;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 24px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 90px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~ a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.username-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;


        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;


        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  width: 100%;
  color: #fff;
}
</style>